package com.gnerv.sylvanas.module.sse.controller;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.module.sse.logic.SseLogicService;
import com.gnerv.sylvanas.module.sse.model.dto.SseMessageDTO;
import com.gnerv.sylvanas.module.sse.model.dto.SubscribeDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @author ligen
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/sse/subscribe")
public class SseController {

    private final SseLogicService sseLogicService;

    @PostMapping(value = "")
    public SseEmitter createSubscribe(@RequestBody SubscribeDTO subscribeDTO) {
        return sseLogicService.createSubscribe(subscribeDTO);
    }

    @PutMapping(value = "")
    public SseEmitter updateSubscribe(@RequestBody SubscribeDTO subscribeDTO) {
        return sseLogicService.updateSubscribe(subscribeDTO);
    }

    @DeleteMapping(value = "")
    public SseEmitter deleteSubscribe(@RequestBody SubscribeDTO subscribeDTO) {
        return sseLogicService.deleteSubscribe(subscribeDTO);
    }

    @PostMapping("/send")
    public RestResult<Void> sendMessage(@RequestBody SseMessageDTO sseMessageDTO) {
        return sseLogicService.sendMessage(sseMessageDTO);
    }

}
